<?php
class UsersController extends AppController{
	//var $name='Users';
	var $helpers = array('Javascript');
	var $components=array('Auth');		
	function beforeFilter(){    
		//parent::beforeFilter();     
		$this->Auth->allow('*');
		//$this->Auth->fields=array('username'=>'username','password'=>'secretword');
	}	
	function delete(){
		$id=$_POST['id'];
		$this->data['User']['id']=$id;
		$this->User->delete($this->data);
	}
	function edit($id){
		$data=$this->User->findById($id);
		$password=$data['User']['password'];
		if(!empty($this->data)){
			$this->data['User']['id']=$id;
			$this->data['User']['password']=$password;
			if($this->User->save($this->data)){			
				$this->redirect("/users/");
			}			
		}		
		$this->set('data', $data);
		$this->set('id', $id);
	}
	function index(){
		$data=$this->User->findAll();
		$this->set('data', $data);
		$this->set('i',1);		
	}
	function logoff(){
		$this->Session->destroy();
		$this->redirect('/');	
	}
    function login(){
        $this->set('msg', 'Please enter username/password.');
        if(!empty($this->data)){
        	if($this->Auth->login($this->data)>0){
        		$data=$this->User->authenticate($this->data['User']['username'], $this->data['User']['password']);
        		$this->Session->write('userid', $data['User']['id']);
        		$this->Session->write('username', $data['User']['username']);
        		$this->Session->write('userlevel', $data['User']['level']);
        		
        		$this->redirect('/patients/index');
        	}
        	else{
        		$this->set('msg', '<label style="color:red">Invalid username/password.</label>');
        	}        	
        }        
        else{
        	$this->set('msg', 'Please enter username/password.');	
        }
    }    
    function add(){
    	if(!empty($this->data)){
    		//$this->data['User']['password']=sha1($this->data['User']['password']);
    		if($this->User->save($this->data)){
    			$this->redirect('/users/');
    		}
    		else{    			
    		}
    	}
    }
}
?>